package com.fireline.beauty.frame;

import java.util.ArrayList;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.fireline.beauty.ContextHelp;
import com.fireline.beauty.cashier.CashierHomeView;
import com.fireline.beauty.image.ImageUtil;
import com.zgw.fireline.base.dataset.Dataset;

import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

public class MainWindow extends ApplicationWindow {
	private Action action;
	private Action action_1;
	private Composite content;

	/**
	 * Create the application window.
	 */
	public MainWindow() {
		super(null);
		addToolBar(SWT.FLAT | SWT.WRAP);
		addMenuBar();
		addStatusLine();
	}

	/**
	 * Create contents of the application window.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		setStatus("");
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(2, false));

		ToolBar toolBar = new ToolBar(container, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				2, 1));

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.setText("每日提醒");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.setText("查看日志");

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.setText("更换用户");

		ToolItem tltmNewItem_3 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_3.setText("软件帮助");

		ToolItem tltmNewItem_4 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_4.setText("退出系统");

		ToolBar toolBar_1 = new ToolBar(container, SWT.FLAT | SWT.RIGHT
				| SWT.VERTICAL);
		toolBar_1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false,
				1, 1));

		ToolItem tltmNewItem_5 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_5.setText("前台收银");
		tltmNewItem_5.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				event.data = "cashier";
				handEvent(event);
			}
		});

		ToolItem tltmNewItem_6 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_6.setText("会员管理");
		tltmNewItem_6.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				event.data = "member";
				handEvent(event);
			}
		});

		ToolItem tltmNewItem_7 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_7.setText("库存管理");
		tltmNewItem_7.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				event.data = "product";
				handEvent(event);
			}
		});

		ToolItem tltmNewItem_8 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_8.setText("统计报表");
		tltmNewItem_8.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				event.data = "";
				handEvent(event);
			}
		});

		ToolItem tltmNewItem_9 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_9.setText("系统设置");
		tltmNewItem_9.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				event.data = "seting";
				handEvent(event);
			}
		});
		content = new Composite(container, SWT.NONE);
		content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		content.setLayout(new StackLayout());

		return container;
	}

	private void handEvent(Event e) {
		if ("cashier".equals(e.data)) {
			doShowCashier();
		} else if ("member".equals(e.data)) {
			doShownavigation("member");
		} else if ("product".equals(e.data)) {
			doShownavigation("product");
		} else if ("seting".equals(e.data)) {
			doShownavigation("seting");
		}
	}

	/*
	 * 显示菜单管理界面
	 */
	private void doShownavigation(String type) {
		Control old = (Control) content.getData(type);
		if (old == null || old.isDisposed()) {
			Composite parent = new Composite(content, SWT.BORDER);
			parent.setLayout(new GridLayout(1, false));
			Composite body = new Composite(parent, SWT.NONE);
			GridLayout gl_composite_3 = new GridLayout(2, true);
			gl_composite_3.horizontalSpacing = 50;
			body.setLayout(gl_composite_3);
			body.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true,
					1, 1));
			Dataset set = ContextHelp.getFunctions(type);
			final SelectionListener listener = new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					FunctionComposite fun = (FunctionComposite) e.widget;
					ContextHelp.OpenFunction(getShell(), fun.id, null);
				}
			};
			for (int i = 0; i < set.getCount(); i++) {
				set.setSelected(i);
				FunctionComposite comp = new FunctionComposite(body,
						(Image) null, set.getString("NAME"),
						set.getString("describe"), set.getString("id"));
				comp.setLayoutData(new GridData(310, 50));
				comp.addSelectionListener(listener);
			}
			content.setData(type, parent);
		}
		StackLayout layout = (StackLayout) content.getLayout();
		layout.topControl = (Control) content.getData(type);
		content.layout(true);
	}

	/*
	 * 显示收银界面
	 */
	private void doShowCashier() {
		Control old = (Control) content.getData("cashier");
		if (old == null || old.isDisposed()) {
			content.setData("cashier",
					new CashierHomeView().createControl(content));
		}
		StackLayout layout = (StackLayout) content.getLayout();
		layout.topControl = (Control) content.getData("cashier");
		content.layout(true);
	}

	/**
	 * Create the menu manager.
	 * 
	 * @return the menu manager
	 */
	@Override
	protected MenuManager createMenuManager() {
		MenuManager menuManager = new MenuManager("menu");
		return menuManager;
	}

	/**
	 * Create the toolbar manager.
	 * 
	 * @return the toolbar manager
	 */
	@Override
	protected ToolBarManager createToolBarManager(int style) {
		ToolBarManager toolBarManager = new ToolBarManager(style);
		return toolBarManager;
	}

	/**
	 * Create the status line manager.
	 * 
	 * @return the status line manager
	 */
	@Override
	protected StatusLineManager createStatusLineManager() {
		StatusLineManager statusLineManager = new StatusLineManager();
		return statusLineManager;
	}

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			MainWindow window = new MainWindow();
			window.setBlockOnOpen(true);
			window.open();
			Display.getCurrent().dispose();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Configure the shell.
	 * 
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("New Application");
	}

	/**
	 * Return the initial size of the window.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(623, 633);
	}
}
